using System;
using System.Xml;

using ScenarioFramework.WorkflowEngine.DataModel;

namespace ScenarioFramework.Modules.PCM.DataModel.ConfigurationManager.MetaWorkflow
{
	//*********************************************************************
	//
	// XMLMetaWorkflow Class
	//
	// The MetaWorkflow class represents a scenario pre-workflow that stores
	// configuring data in a file before creating definitive workflow model
	// to run in product configuration.
	//
	// MetaWorkflow rappresents a selection procedure for configuring a product
	//
	//*********************************************************************

	public class XMLMetaWorkflow : bOSdotNET.XML.Data.XMLObject
	{
		

		public const string MAIN_NODE	= "MetaWorkflow";

		private const string NAME_ATTRIBUTE		= "Name";
		private const string DESCRIPTION_NODE	= "Description";
		private const string DEFAULT_HELP_URL_NODE	= "DefaultHelpUrl";

		/** PRIVATE FIELDS **/

		private String sName;
		private String sDescription;
		private string sDefaultHelpUrl = string.Empty;

		private ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers vUsers;
        private ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles vRoles;
		private XMLMetaWorkflowActivities vActivities;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple mwf **/
		public XMLMetaWorkflow() 
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLMetaWorkflow(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		/** PROPERTIES **/

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public string Description 
		{
			get { return sDescription; }
			set { sDescription = value; }
		}

		public string DefaultHelpUrl
		{
			get { return sDefaultHelpUrl;}
			set { sDefaultHelpUrl = value;}
		}

        public ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers XMLUsers
		{
			get { return vUsers; }
			set { vUsers = value; }
		}

        public ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles XMLRoles
		{
			get { return vRoles; }
			set { vRoles = value; }
		}

		public XMLMetaWorkflowActivities XMLMetaWorkflowActivities 
		{
			get { return vActivities; }
			set { vActivities = value; }
		}


		/** INSTANCE METHODS **/

		override public void Init()
		{
			vUsers = new ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers();
			vRoles = new ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles();

			vActivities = new XMLMetaWorkflowActivities();

			sName=string.Empty;
			sDescription=string.Empty;
		}

		/** Parsering method for filling the mwf **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name)
				{
					case XMLMetaWorkflow.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;
				}
			}
			
			/** find the sub tags witch contains mwf values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch (node.Name) 
				{
					case XMLMetaWorkflow.DESCRIPTION_NODE:
						sDescription = node.InnerText;
						break;

					case ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers.MAIN_NODE:
						vUsers.Parse(node);
						break;

					case ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles.MAIN_NODE:
						vRoles.Parse(node);
						break;

					case XMLMetaWorkflowActivities.MAIN_NODE:
						vActivities.Parse(node);
						break;

					case XMLMetaWorkflow.DEFAULT_HELP_URL_NODE:
						sDefaultHelpUrl = node.InnerText;
						break;
				} 
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLMetaWorkflow.MAIN_NODE);

			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLMetaWorkflow.NAME_ATTRIBUTE, sName) );
			xmlNode.AddNode ( new bOSdotNET.XML.XMLNode (XMLMetaWorkflow.DESCRIPTION_NODE, sDescription) );

			xmlNode.AddNode ( vActivities.GetXMLNode() );
			xmlNode.AddNode ( vUsers.GetXMLNode() );
			xmlNode.AddNode ( vRoles.GetXMLNode() );

			return xmlNode;
		}

		public void AddMetaWorkflow(XMLMetaWorkflow xmlMetaWorkflow)
		{
			for ( int i=0; i<xmlMetaWorkflow.XMLMetaWorkflowActivities.Count;i++)
			{
				vActivities.Add ( xmlMetaWorkflow.XMLMetaWorkflowActivities[i]);
			}
		}
	}
}
